package com.paas.service.modules.sso.cas.client.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;
import org.jasig.cas.client.authentication.AuthenticationFilter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class LoginController {
	/*
	 * @Autowired User user;
	 */
	/**
	 * 用户登录
	 * 
	 * @param user
	 *            　登录用户
	 * @return
	 */
	@RequestMapping(value = "login",params = "main")
	public ModelAndView login( HttpSession session,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
//		response.setContentType("text/html");
//	    PrintWriter out = response.getWriter();
//	    out.println("<html>");
//	    out.println("<head>");
//	    out.println("<title>Welcome to cas shirodemo sample System!</title>");
//	    out.println("</head>");
//	    out.println("<body>");
//	    out.println("<h1>Welcome to shirodemo sample System!</h1>");
//	    CASFilterRequestWrapper  reqWrapper=new CASFilterRequestWrapper(request);
//	    out.println("<p>The logon user:" + reqWrapper.getRemoteUser() + "</p>");
//	    HttpSession session2=request.getSession();
//	    out.println("<p>The logon user:" + 
//	                   session2.getAttribute(CASFilter.CAS_FILTER_USER)  + "</p>");
//	    out.println("<p>The logon user:" + 
//	         session2.getAttribute("edu.yale.its.tp.cas.client.filter.user") + "</p>");
//	    out.println("</body>");
//	    out.println("</html>");
		 ModelAndView modelView = new ModelAndView();
		Subject currentUser = SecurityUtils.getSubject();
//		Principals principals = currentUser.getPrincipals();
//		 AttributePrincipal principal = (AttributePrincipal)
//		 request.getUserPrincipal();
//		 String username = principal.getName();
//		 UsernamePasswordToken token = new UsernamePasswordToken(
//		 user.getUsercode(), EncryptUtils.encryptMD5(user.getPassword()));
//		 //身份设置 设置是否允许跨会话
//		 token.setRememberMe(true);
//		 try {
//		 //这里提交验证 会调用 MonitorRealm.doGetAuthenticationInfo回去身份认证信息
//		 currentUser.login(token);
//		 } catch (AuthenticationException e) {
//		 modelView.addObject("message", "login errors");
//		 modelView.setViewName("/login");
//		 e.printStackTrace();
//		
//		 }
		// 验证身份是否通过
		 if(currentUser.isAuthenticated()){
		 modelView.setViewName("/main");
		 }else{
		 modelView.addObject("message", "login errors");
		 modelView.setViewName("/login");
		 }
		 return modelView;
	}

	/**
	 * 退出登录
	 * 
	 * @return
	 */
	@RequestMapping(value = "logout")
	public String logout() {
		Subject currentUser = SecurityUtils.getSubject();
		try {
			// 结束回话以及授权数据
			currentUser.logout();
		} catch (AuthenticationException e) {
			e.printStackTrace();

		}
		return "/login";
	}

	@RequestMapping(value = "login",params = "myjsp")
	public ModelAndView login2() {

		System.out.println("sss");
		ModelAndView modelView = new ModelAndView();
		modelView.addObject("message", "登录成功!");
		modelView.setViewName("/my");
		return modelView;
	}

	@RequestMapping(value = "login",params = "test")
	public ModelAndView login3() {
		System.out.println("sss");
		ModelAndView modelView = new ModelAndView();
		modelView.addObject("message", "登录成功!");
		modelView.setViewName("/test");
		return modelView;
	}
}
